package Object.StudentManager;
import java.util.Scanner;
import java.util.List;

public class ConsoleUI {
    private final Scanner inputScanner = new Scanner(System.in);
    private final StudentManager studentManager;

    public ConsoleUI(StudentManager studentManager) {
        this.studentManager = studentManager;
    }

    public void printMenu() {
        System.out.println("\n===== 学生管理系统 =====");
        System.out.println("1. 添加学生");
        System.out.println("2. 删除学生");
        System.out.println("3. 修改学生信息");
        System.out.println("4. 查看所有学生");
        System.out.println("5. 查找学生");
        System.out.println("0. 退出系统");
        System.out.print("请选择操作: ");
    }

    public void showMainMenu() {
        boolean running = true;
        while (running) {
            printMenu();
            int choice = inputScanner.nextInt();

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    displayAllStudents();
                    break;
                case 5:
                    searchStudent();
                    break;
                case 0:
                    exit();
                    running = false;
                    break;
                default:
                    System.out.println("无效选择，请重新输入!");
            }
        }
    }

    private void addStudent() {
        System.out.println("\n--- 添加学生 ---");
        System.out.print("输入学号: ");
        String id = inputScanner.nextLine();
        System.out.print("输入姓名: ");
        String name = inputScanner.nextLine();
        System.out.print("输入年龄: ");
        int age = inputScanner.nextInt();
        inputScanner.nextLine(); // 消耗换行符

        Student student = new Student(id, name, age);
        if (studentManager.addStudent(student)) {
            System.out.println("添加学生成功!");
        } else {
            System.out.println("添加失败，学号已存在!");
        }
    }

    private void deleteStudent() {
        System.out.println("\n--- 删除学生 ---");
        System.out.print("输入要删除的学生学号: ");
        String id = inputScanner.nextLine();

        if (studentManager.deleteStudent(id)) {
            System.out.println("删除学生成功!");
        } else {
            System.out.println("删除失败，学号不存在!");
        }
    }

    private void updateStudent() {
        System.out.println("\n--- 修改学生信息 ---");
        System.out.print("输入要修改的学生学号: ");
        String id = inputScanner.nextLine();

        Student existing = studentManager.getStudentById(id);
        if (existing == null) {
            System.out.println("学生不存在!");
            return;
        }

        System.out.println("当前信息: " + existing);
        System.out.print("输入新姓名(留空不修改): ");
        String name = inputScanner.nextLine();
        System.out.print("输入新年龄(输入0不修改): ");
        int age = inputScanner.nextInt();
        inputScanner.nextLine(); // 消耗换行符

        // 只更新有变化的字段
        if (!name.isEmpty()) {
            existing.setName(name);
        }
        if (age != 0) {
            existing.setAge(age);
        }

        System.out.println("修改成功!");
    }

    private void displayAllStudents() {
        System.out.println("\n--- 所有学生列表 ---");
        List<Student> students = studentManager.getAllStudents();
        if (students.isEmpty()) {
            System.out.println("没有学生记录!");
        } else {
            for (Student student : students) {
                System.out.println(student);
            }
        }
    }

    private void searchStudent() {
        System.out.println("\n--- 查找学生 ---");
        System.out.print("输入学号或姓名: ");
        String keyword = inputScanner.nextLine();

        List<Student> results = studentManager.searchStudents(keyword);
        if (results.isEmpty()) {
            System.out.println("没有找到匹配的学生!");
        } else {
            System.out.println("找到 " + results.size() + " 个匹配结果:");
            for (Student student : results) {
                System.out.println(student);
            }
        }
    }

    private void exit() {
        System.out.println("感谢使用学生管理系统，再见!");
        inputScanner.close();
    }
}
